﻿using System;
using System.Text;
using System.IO;
using System.Net;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Subgurim.Controles;
using Data;
using Business;

namespace GestorEventos
{
    public partial class editPalco : System.Web.UI.Page
    {
        static double longitude;
        static double latitude;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["palcoID"] != null)
            {

                int palcoID = Convert.ToInt32(Request.QueryString["palcoID"].ToString());
                GMap1.enableGoogleBar = true;
                GMap1.enableHookMouseWheelToZoom = true;
                GMap1.enableGKeyboardHandler = true;
                GMap1.addControl(new GControl(GControl.extraBuilt.MarkCenter));
                //       GMap1.addControl(new GControl(GControl.extraBuilt.TextualCoordinatesControl));
                GMap1.setCenter(new GLatLng(40, -8), 6);
                GMap1.addControl(new GControl(GControl.preBuilt.MapTypeControl, new GControlPosition(GControlPosition.position.Bottom_Right)));
                GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl, new GControlPosition(GControlPosition.position.Top_Right)));

                Palco p = new Palco();
                p = p.getPalcoByID(palcoID);
                GMarker marker = new GMarker(new GLatLng(p.getCoordLat(), p.getCoordLon()));
                GMap1.addGMarker(marker);
                GMap1.setCenter(new GLatLng(p.getCoordLat(), p.getCoordLon()), 16);
                if (!Page.IsPostBack)
                {
                    this.Nometb.Text = p.getNomePalco();
                    this.Localidadetb.Text = p.getLocalidade();
                    this.Desctb.Text = p.getDescricaoPalco();
                    this.Categoriatb.Text = p.getCategoria();
                    this.Ruatb.Text = p.getRua();
                    this.CodPostaltb.Text = p.getCodPostal();
                }
                else
                {
                    p.setNomePalco(this.Nometb.Text);
                    p.setDescricaoPalco(this.Desctb.Text);
                    p.setLocalidade(this.Localidadetb.Text);
                    p.setCategoria(this.Categoriatb.Text);
                    p.setCodPostal(this.CodPostaltb.Text);
                    p.setRua(this.Ruatb.Text);
                    p.setCoordLat((float)latitude);
                    p.setCoordLon((float)longitude);
                    bool i = p.updatePalco();
                    GMap1.setCenter(new GLatLng(latitude, longitude), 14);
                }
            }
            else {
                Response.Redirect("Default.aspx");
            }

        }

        protected void Confirmarb_Click(object sender, EventArgs e)
        {

            if (Request.QueryString["palcoID"] != null)
            {

                int palcoID = Convert.ToInt32(Request.QueryString["palcoID"].ToString());
                Palco p = new Palco();
                p = p.getPalcoByID(palcoID);
                p.setNomePalco(this.Nometb.Text);
                p.setDescricaoPalco(this.Desctb.Text);
                p.setLocalidade(this.Localidadetb.Text);
                p.setCategoria(this.Categoriatb.Text);
                p.setCodPostal(this.CodPostaltb.Text);
                p.setRua(this.Ruatb.Text);
                p.setCoordLat((float)latitude);
                p.setCoordLon((float)longitude);
                bool i = p.updatePalco();

                if (i)
                {
                    Response.Redirect("showPalco.aspx?palcoID=" + palcoID + "");
                }
                else {
                    Response.Redirect("editPalco.aspx?palcoID=" + palcoID + "");
                }
            }
         
        }

        protected void Sairb_Click(object sender, EventArgs e)
        {
            
        }

        protected void Verificarb_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append(this.Ruatb.Text + ", " + this.Localidadetb.Text);
            float lat, longi;
            HttpWebRequest a = (HttpWebRequest)WebRequest.Create("http://maps.google.com/maps/geo?q=" + s.ToString() + "&output=csv&oe=utf8&sensor=true&key=ABQIAAAAp9PE6dkYqHKQfkAlDcxiTxRqasZdLy4IhB39S8xAqTahPYA7dxToPocIAu-_Po_hv5Hv1kc2-8gGvQ");
            HttpWebResponse resp = (HttpWebResponse)a.GetResponse();
            Stream st = resp.GetResponseStream();
            StreamReader sr = new StreamReader(st);
            string buffer = sr.ReadToEnd();
            String[] items = buffer.Split(',');
            if (items[0].Equals("200"))
            {
                lat = float.Parse(items[2].Replace(".", ","));
                longi = float.Parse(items[3].Replace(".", ","));
            }
            else
            {
                lat = -200;
                longi = -200;
            }

            GMap1.setCenter(new GLatLng((double)lat, (double)longi), 16);
        }

        protected string GMap1_Click(object s, GAjaxServerEventArgs e)
        {
            GMarker marker = new GMarker(e.point);

            GInfoWindow window = new GInfoWindow(marker,
                string.Format(@"
        <b>Coordenadas</b><br />
        Latitude = {0}<br/>
        Longitude = {1}
        ",
                e.point.lat.ToString(),
                e.point.lng.ToString())
            , true);
            latitude = e.point.lat;
            longitude = e.point.lng;

            return window.ToString(e.map);
        }

        protected string GMap1_MarkerClick(object s, GAjaxServerEventArgs e)
        {
            string markerID = GMap1.getGMapElementById(e.who);
            string js = string.Format(@"
    if ({0}.isHidden())
        {0}.show()
    else
        {0}.hide();
    ", markerID);

            return js;
        } 
    }
}
